home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xarchie-2.0.9 / hostname.c < prev    next >
C/C++ Source or Header  |  1995-06-18  |  1KB  |  50 lines

  1. /*
  2.  * hostname.c : Amamzing how hard it is to get this information...
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  *
  6.  * Many possibilities here. The current attempt is:
  7.  * (a) Use HOSTNAME if set, since SO many people have strange systems
  8.  * (b) else call gethostname(), complain if it fails
  9.  * (c) if it succeeded, call gethostbyname() to "canonicalize"
  10.  *     the name (many systems don't return a fully-qualified name,
  11.  *     and getdomainname() is a loss.
  12.  */
  13. #include <stdio.h>
  14. #include <netdb.h>
  15. #ifndef MAXHOSTNAMELEN
  16. #include <sys/param.h>
  17. #define MAXHOSTNAMELEN 64        /* same arbitrary limit as in libwww */
  18. #endif
  19. #include "sysdefs.h"
  20. #include "stringdefs.h"
  21.  
  22. char *
  23. GetHostname()
  24. {
  25.     static char hostname[MAXHOSTNAMELEN];
  26.     struct hostent *host;
  27.  
  28.     if (getenv("HOSTNAME") != NULL)
  29.     strcpy(hostname,getenv("HOSTNAME"));
  30.     else if (gethostname(hostname,sizeof(hostname)) != 0) {
  31.     fprintf(stderr,"gethostname failed -- you should set $HOSTNAME");
  32.     strcpy(hostname,"unknown.host");
  33.     } else if ((host=gethostbyname(hostname)) == NULL) {
  34.     fprintf(stderr,"gethostbyname failed -- you should set $HOSTNAME");
  35.     strcpy(hostname,"unknown.host");
  36.     } else {
  37.     strcpy(hostname,host->h_name);
  38.     }
  39.     return(hostname);
  40. }
  41.  
  42. #ifdef STANDALONE
  43. main()
  44. {
  45.     printf("%s\n",GetHostname());
  46.     exit(0);
  47. }
  48. #endif /* STANDALONE */
  49.  
  50.